package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.User;
import com.zr.service.UpdatePswService;
/**
 * 修改个人密码的后台
 * @author 周钟鸣
 *
 */
public class UpdatePswController extends HttpServlet{
	UpdatePswService ups=new UpdatePswService();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String oldpsw=req.getParameter("oldpsw");
		String newpsw=req.getParameter("newpsw");
		String newpswagain=req.getParameter("newpswagain");
		HttpSession session=req.getSession();
		//从session获取当前用户的对象
		User currentUser = (User) session.getAttribute("currentUser");
		PrintWriter  pw = resp.getWriter();
		resp.setCharacterEncoding("utf8");
		//定义一个临时boolean变量
		boolean  flag ;
		//判断传入后台的原密码和session中获取的密码是否一致
	     if(oldpsw.equals(currentUser.getUpsw())){
	    	 //调用提供修改密码的服务
	    	flag =ups.updataPswService(newpsw, currentUser.getUname());
	    	//判断密码修改成功与否
	    	if(flag){
	    		pw.write(String.valueOf(flag)); 
	    	}else{
	    		String str="0";
	    		pw.write(str); 
	    	}
	     }else{
	    	flag=false;
	    	pw.write(String.valueOf(flag));
	     }
	}
}
